home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Mousetools / PortSwitch / Source / PortSwitch.C < prev   
Encoding:
C/C++ Source or Header  |  1996-09-27  |  6.3 KB  |  254 lines

  1. /*
  2. **  $Filename: utilities/portswitch.c $
  3. **  $Release: 1.2 $
  4. **  $Revision: 1.2 $
  5. **
  6. **  Utility to switch mouse control to a different port
  7. **
  8. **  © Software Ambience 1995/96
  9. **  All Rights Reserved
  10. */
  11.  
  12. /****i* PortSwitch **************************************************
  13. *
  14. *   NAME
  15. *       PortSwitch -- Switch mouse control to a different port.
  16. *
  17. *   COPYRIGHT
  18. *       © Software Ambience 1995/96
  19. *
  20. *   PROGRAMMER
  21. *       Steve Sloggett of Software Ambience
  22. *
  23. *   STATUS
  24. *       Freeware
  25. *
  26. *   LANGUAGE
  27. *       C
  28. *
  29. *   CURRENT VERSION
  30. *       1.2
  31. *
  32. *   USAGE
  33. *       PortSwitch PORT/N/A
  34. *
  35. *   HISTORY
  36. *       1.0 : 28th December 1995 : Initial version
  37. *       1.1 : 29th December 1995 : Added command line parameters and
  38. *                                  various code improvements
  39. *       1.2 : 17th February 1996 : Miscellaneous changes
  40. *
  41. *   NOTES
  42. *       Requires Kickstart 2.0 or higher.
  43. *
  44. *       This source code can be used as part of any program provided
  45. *       that I am given credit for my work by means of the following
  46. *       statement:
  47. *
  48. *           Part of this program is based upon Port Switch v1.2
  49. *           which is © Software Ambience 1995/96
  50. *
  51. *       I would also appreciate being notified of this fact. Thanks.
  52. *
  53. **********************************************************************
  54. *
  55. */
  56.  
  57. #include <exec/types.h>
  58. #include <exec/execbase.h>
  59. #include <dos/dos.h>
  60. #include <dos/rdargs.h>
  61. #include <devices/input.h>
  62.  
  63. #include <clib/alib_protos.h>
  64. #include <clib/dos_protos.h>
  65. #include <clib/exec_protos.h>
  66.  
  67. /* Function prototypes */
  68. void Terminate(STRPTR message, UBYTE code);
  69.  
  70. /* Personal habit :-) */
  71. #define NOT !
  72.  
  73. /* Kickstart identification */
  74. #define KICKSTART_2_0 36
  75.  
  76. /* Option identification */
  77. #define OPTION_TEMPLATE "PORT/N/A"
  78.  
  79. #define OPTION_PORT  0
  80. #define OPTION_COUNT 1
  81.  
  82. /* Library bases */
  83. extern struct ExecBase *SysBase;
  84.  
  85. struct Library *DosBase = NULL;
  86.  
  87. /* Define program version string for use by command: version */
  88. STRPTR version = "\0$VER: PortSwitch 1.2 (17.02.96)";
  89.  
  90. /****** PortSwitch/main **********************************************
  91. *
  92. *   NAME
  93. *       main -- Logical start and end of the program.
  94. *
  95. *   SYNOPSIS
  96. *       main()
  97. *
  98. *       void main(void);
  99. *
  100. *   FUNCTION
  101. *       main() is the point where execution logically begins and ends.
  102. *
  103. *   INPUTS
  104. *       void
  105. *
  106. *   RESULT
  107. *       void
  108. *
  109. *   EXAMPLE
  110. *
  111. *   NOTES
  112. *
  113. *   BUGS
  114. *
  115. *   SEE ALSO
  116. *
  117. **********************************************************************
  118. *
  119. */
  120.  
  121. void main(void)
  122. {
  123.     BYTE port;
  124.     LONG options[OPTION_COUNT];
  125.     struct IOStdReq *request;
  126.     struct MsgPort *messagePort;
  127.     struct RDArgs *arguments;
  128.  
  129.     /* Default options */
  130.     options[OPTION_PORT] = 0;
  131.     /* Display program credits */
  132.     printf("\nPort Switch v1.2 © Software Ambience 1995/96 (Freeware)\n");
  133.     printf("Programmed by Steve Sloggett\n\n");
  134.     /* Check if Kickstart 2.0 or higher is available */
  135.     if(SysBase->LibNode.lib_Version < KICKSTART_2_0)
  136.         Terminate("This program requires Kickstart 2.0 or higher", RETURN_WARN);
  137.     /* Open dos.library */
  138.     DosBase = OpenLibrary("dos.library", 36);
  139.     if(NOT DosBase)
  140.         /* Terminate execution */
  141.         Terminate("Unable to open dos.library v36 or higher", RETURN_FAIL);
  142.     /* Obtain the options supplied from the command line */
  143.     arguments = ReadArgs(OPTION_TEMPLATE, options, NULL);
  144.     /* Obtain the new mouse control port number */
  145.     port = *((LONG *) options[OPTION_PORT]);
  146.     /* Check the options supplied from the command line are valid */
  147.     if(NOT arguments || (port != 0 && port != 1))
  148.     {
  149.         /* Close dos.library */
  150.         CloseLibrary(DosBase);
  151.         /* Terminate execution */
  152.         Terminate("Mouse control port number is invalid", RETURN_FAIL);
  153.     }
  154.     /* Create the message port */
  155.     messagePort = CreatePort(NULL, 0);
  156.     if(NOT messagePort)
  157.     {
  158.         /* Free the arguments */ 
  159.         FreeArgs(arguments);
  160.         /* Close dos.library */
  161.         CloseLibrary(DosBase);
  162.         /* Terminate execution */
  163.         Terminate("Unable to create a message port", RETURN_FAIL);
  164.     }
  165.     /* Create the request structure */
  166.     request = CreateStdIO(messagePort);
  167.     if(NOT request)
  168.     {
  169.         /* Remove the message port */
  170.         DeletePort(messagePort);
  171.         /* Free the arguments */ 
  172.         FreeArgs(arguments);
  173.         /* Close dos.library */
  174.         CloseLibrary(DosBase);
  175.         /* Terminate execution */
  176.         Terminate("Unable to create a request", RETURN_FAIL);
  177.     }
  178.     /* Open the input device */
  179.     if(OpenDevice("input.device", 0, request, NULL) != 0)
  180.     {
  181.         /* Free the request */
  182.         DeleteStdIO(request);
  183.         /* Remove the message port */
  184.         DeletePort(messagePort);
  185.         /* Free the arguments */ 
  186.         FreeArgs(arguments);
  187.         /* Close dos.library */
  188.         CloseLibrary(DosBase);
  189.         /* Terminate execution */
  190.         Terminate("Unable to open input.device", RETURN_FAIL);
  191.     }
  192.     /* Create the request to change the mouse port */
  193.     request->io_Message.mn_ReplyPort = messagePort;
  194.     request->io_Command = IND_SETMPORT;
  195.     request->io_Flags = NULL;
  196.     request->io_Length = 1;
  197.     request->io_Data = &port;
  198.     /* Submit the request and wait for it to be carried out */
  199.     DoIO(request);
  200.     /* Close the input device */
  201.     CloseDevice(request);
  202.     /* Free the request */
  203.     DeleteStdIO(request);
  204.     /* Remove the message port */
  205.     DeletePort(messagePort);
  206.     /* Free the arguments */ 
  207.     FreeArgs(arguments);
  208.     /* Close dos.library */
  209.     CloseLibrary(DosBase);
  210.     /* Terminate execution */
  211.     Terminate("Mouse control port is now changed", RETURN_OK);
  212. }
  213.  
  214. /****** PortSwitch/Terminate *****************************************
  215. *
  216. *   NAME
  217. *       Terminate -- Display an error message and return to AmigaDOS.
  218. *
  219. *   SYNOPSIS
  220. *       Terminate(message, code)
  221. *
  222. *       void Terminate(STRPTR, UBYTE);
  223. *
  224. *   FUNCTION
  225. *       Terminate() displays the supplied error message and then
  226. *       terminates the program with the error code sprecified.
  227. *
  228. *   INPUTS
  229. *       message - Error message to be displayed.
  230. *       code    - Error code to return to AmigaDOS.
  231. *
  232. *   RESULT
  233. *       void
  234. *
  235. *   EXAMPLE
  236. *
  237. *   NOTES
  238. *
  239. *   BUGS
  240. *
  241. *   SEE ALSO
  242. *
  243. **********************************************************************
  244. *
  245. */
  246.  
  247. void Terminate(STRPTR message, UBYTE code)
  248. {
  249.     /* Display termination message */
  250.     printf("%s\n\n", message);
  251.     /* Return error code to AmigaDOS */
  252.     exit(code);
  253. }
  254.